package com.quick.service.impl;

import java.util.List;

import com.quick.base.Page;
import com.quick.shop.doamin.vo.ShopGoodsViewVo;
import com.quick.shop.doamin.vo.ShopGoodsVo;
import com.quick.util.PageUtil;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.quick.shop.doamin.po.ShopGoods;
import com.quick.shop.doamin.po.ShopGoodsImages;
import com.quick.shop.doamin.search.ShopGoodsSearch;
import com.quick.base.Search;
import com.quick.mapper.ShopGoodsImagesMapper;
import com.quick.mapper.ShopGoodsMapper;
import com.quick.service.IShopGoodsService;
@Service
public class ShopGoodsServiceImpl implements IShopGoodsService {
	
	@Autowired
	private ShopGoodsMapper shopGoodsMapper;
	@Autowired
	private ShopGoodsImagesMapper goodsImagesMapper;

	@Override
	public Page<ShopGoodsVo> search(Search<ShopGoodsSearch> search) {
		PageHelper.startPage(search.getPageNum(), search.getPageSize());
		QueryWrapper<ShopGoods> queryWrapper =  new QueryWrapper<ShopGoods>();
		queryWrapper.lambda().orderByDesc(ShopGoods::getCreateTime);
		List<ShopGoods> list = shopGoodsMapper.selectList(queryWrapper);
		PageInfo<ShopGoods>  result = new PageInfo<ShopGoods>(list);
		return PageUtil.page(result,ShopGoodsVo.class);
	}

	@Override
	public ShopGoodsViewVo queryId(Long id) {
		ShopGoodsViewVo viewVo = new ShopGoodsViewVo();
		QueryWrapper<ShopGoods> queryWrapper =  new QueryWrapper<ShopGoods>();
		queryWrapper.lambda().eq(ShopGoods::getId, id);
		ShopGoods source = shopGoodsMapper.selectOne(queryWrapper);
		BeanUtils.copyProperties(source, viewVo);
		QueryWrapper<ShopGoodsImages> wrapper =  new QueryWrapper<ShopGoodsImages>();
		wrapper.lambda().eq(ShopGoodsImages::getGoodsId, id);
		wrapper.lambda().eq(ShopGoodsImages::getImgType, "01");
		List<ShopGoodsImages> goodsImages = goodsImagesMapper.selectList(wrapper);
		viewVo.setGoodsImages(goodsImages);
		return viewVo;
	}

}
